package com.example.Laboratory.Service.Impl;


import com.example.Laboratory.Entity.Organization;
import com.example.Laboratory.Mapper.OrganizationMapper;
import com.example.Laboratory.Service.OrganizationService;
import com.example.Laboratory.Untils.OperationFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    OrganizationMapper organizationMapper;


    //上传图片
    @Override
    public String addOrganization(MultipartFile file) {
        if (organizationMapper.researchOrganization2() < 1) {
            String path = OperationFile.upfile(file);
            if (path.equals("文件已存在")) {
                return path;
            } else if (path.equals("没有选择文件")) {
                return path;
            } else {
                organizationMapper.addOrganization(path);
                return "上传成功";
            }
        } else {
            return "图片数量超过一张";
        }

    }

    //删除图片
    @Override
    public String deleteOrganization(int organization_id) {
        String filepath = organizationMapper.researchOrganization1(organization_id);
        int code = OperationFile.deletefile(filepath);
        if (code == 1) {
            organizationMapper.deleteOrganization(organization_id);
            return "删除成功";
        } else if (code == 0) {
            return "删除失败";
        } else if (code == 2) {
            return "文件不存在";
        } else {
            return "发生未知错误";
        }
    }

    //查找图片
    @Override
    public Organization researchOrganization() {
        return organizationMapper.researchOrganization();
    }

    //更新图片
    @Override
    public String reviseOrganization(MultipartFile file, int organization_id) {
        String filepath = organizationMapper.researchOrganization1(organization_id);
        String tip = OperationFile.updatafile(filepath, file);
        if (tip.equals("文件已存在")) {
            return tip;
        } else if (tip.equals("没有选择文件")) {
            return tip;
        } else if (tip.equals("更新失败")){
            return tip;
        } else {
            Organization organization = new Organization();
            organization.setOrganization_address(tip);
            organization.setOrganization_id(organization_id);
            organizationMapper.reviseOrganization(organization);
            return "修改成功";
        }
    }
}